
package de.uni.wuppertal.server.ui.support;

import java.awt.BorderLayout;
import java.awt.Component;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import javax.swing.*;

import de.uni.wuppertal.server.LogEntry;

/**
 * @author Martin Schröder
 * @author Patrik Nordsiek
 */
public class LogListCellRenderer implements ListCellRenderer
{
	private static DateFormat dateFormat;
	
	static
	{
		dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
		dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
	}
	
	@Override
	public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus)
	{
		if(value instanceof LogEntry)
		{
			LogEntry entry = (LogEntry)value;
			
			JPanel panel = new JPanel(new BorderLayout());
			panel.setBorder(BorderFactory.createEmptyBorder(1, 3, 1, 3));
			panel.setOpaque(false);
			
			JLabel label = new JLabel(dateFormat.format(entry.getCreated()) + " - " + entry.toString());
			label.setIcon(entry.getIcon());
			
			panel.add(label, BorderLayout.CENTER);
			
			return panel;
		}
		
		if(value == null)
		{
			return new JLabel();
		}
		
		return new JLabel(value.toString());
	}
}
